package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn30500Bo;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.actionform.CarPendingLiftActionForm;
import com.tianqu.tms.www.car.viewhelper.CarPendingLiftViewHelper;


/**
 * @author huangheli
 * 挂起确认
 */
public class CarPendingResultController extends TmsController{
	private Logger logger = Logger.getLogger(CarPendingResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarPendingLiftActionForm form = (CarPendingLiftActionForm)uc.getInputForm();
			//
			Car car = Car.getInstance();
			CarPendingLiftViewHelper viewhelper = new CarPendingLiftViewHelper();
			viewhelper = (CarPendingLiftViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			String serial = viewhelper.getBasicCarId();
			TmsUserProfile profile = uc.getUserProfile();
			car.setUpdateUserId(profile.getUniqueId());
			car.setSerial(viewhelper.getBasicCarId());
			car.updatePendingliftStatus(car,5);
			//
			car = car.findCarBySerial(serial);
			controller.commit();
			 
			//API
			//5、【TMS】挂起/解挂 =》【车代办】同步
			int registerStatus = 5;
			MobileMessage message = new MobileMessage();
			message.putValue("Serial",serial);
			message.setMessage("【TMS】挂起/解挂 =》【车代办】同步,Serial为#Serial#");
			 TransferDataSyn30500Bo api = new TransferDataSyn30500Bo();
			 api.setMobileMessage(message);
			 api.setBarCodeNum(car.getBarCodeNum());
			 api.setSerial(serial);
			 api.setStartTime(StringUtil.getTimeByTimeStamp(car.getTmpStorageTime()));
			 api.setEndTime(StringUtil.getTimeByTimeStamp(car.getTmpEnterTime()));
			 PullingExecutor.getInstance().submit(api);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}



}
